package jwtx

import (
	"github.com/golang-jwt/jwt/v4"
	"time"
)

type Token struct {
	Token  string
	Expire int64
}

// GetJwtToken 生成登录token
func GetJwtToken(secretKey string, seconds int64, otherData map[string]any) (resp *Token, err error) {
	now := time.Now().Unix()
	exp := now + seconds
	claims := make(jwt.MapClaims)
	claims["exp"] = exp
	claims["iat"] = now
	for key, val := range otherData {
		claims[key] = val
	}
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	tokenStr, err := token.SignedString([]byte(secretKey))
	if err == nil {
		resp = &Token{
			Token:  tokenStr,
			Expire: exp,
		}
	}
	return
}
